﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellMessageService_h
#define CellMessageService_h

#include "CellPrerequisites.h"
#include "CellMessageHandler.h"

#if CELL_PLATFORM == CELL_PLATFORM_WIN32
	#include <winsock2.h>
	#include <ws2tcpip.h>
	#include <wspiapi.h>

	/// 定义 Socket 句柄的数据类型。
	#define socket_t SOCKET

#else
	#include <unistd.h>
	#include <cstdlib>
	#include <cstring>
	#include <netdb.h>

	/// 定义 Socket 句柄的数据类型。
	#define socket_t int

	#ifndef INVALID_SOCKET
	#define INVALID_SOCKET -1
	#endif // INVALID_SOCKET

	#ifndef SOCKET_ERROR
	#define SOCKET_ERROR -1
	#endif // SOCKET_ERROR

#endif // CELL_PLATFORM_WIN32

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

// Util functions
void closeSocket(socket_t sock);


/** 消息服务定义。
@author
	Jiangwei Xu
*/
class _CellExport MessageService
{
public:
	/** 构造函数。
	*/
	MessageService(void);

	/** 析构函数。
	*/
	virtual ~MessageService(void);

public:
	/** 返回该消息服务的处理器。
	*/
	virtual MessageHandler* getHandler(void) const;

	/** 设置该消息服务的处理器。
	*/
	virtual void setHandler(MessageHandler* handler);

	/** 定义数据包标志。
	*/
	void defineDataMark(char* headMark, size_t headLength, char* tailMark, size_t tailLength);

	/** 是否定义数据标志。
	*/
	bool existDataMark(void) const;

	/**
	*/
	char* getHeadMark(void) const;
	/**
	*/
	size_t getHeadLength(void) const;

	/**
	*/
	char* getTailMark(void) const;
	/**
	*/
	size_t getTailLength(void) const;

public:
	/** 向指定的会话写入消息。
	*/
	virtual void write(Session* session, Message* message) = 0;

	/** 从指定的会话读取消息。
	*/
	virtual void read(Message* message, Session* session) = 0;

protected:
	MessageHandler* mHandler;

private:
	char* mHeadMark;
	size_t mHeadLength;
	char* mTailMark;
	size_t mTailLength;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellMessageService_h
